package artibella.text 
{
	import artibella.text.StylesManager;
	import caurina.transitions.Tweener;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class CrawlingLabel extends MovieClip
	{
		private var _w:int;
		private var _chp:TextField;
		
		public var indice:int = -1;
		
		private var _auto:Boolean;
		private var _t:Timer;
		
		public function CrawlingLabel( w:int = 100, autorun:Boolean = false ) 
		{
			_auto = autorun;
			_w = w;

			_init();
		}
		
		private function _init():void
		{
			//var format:TextFormat = new TextFormat();
			//format.font = FontsManager.getFontByName( "Arial" );
			//format.size = 10;
			//format.color = 0xFFFFFF;

			_chp = new TextField();
			_chp.defaultTextFormat = StylesManager.instance.getStyleFormat( ".default" );
			_chp.antiAliasType = AntiAliasType.ADVANCED;
			_chp.embedFonts = true;
			_chp.autoSize = "left";
			_chp.wordWrap = false;
			_chp.multiline = false;
			_chp.selectable = false;
			_chp.text = "...";
			//_chp.setTextFormat( format );
			
			_chp.x = 0;
			_chp.y = - _chp.height / 2;
			
			addChild( _chp );
			
			var m:Sprite = new Sprite();
			m.graphics.beginFill( 0xFF0000, 1 );
			m.graphics.drawRect( 0, - _chp.height / 2, _w, _chp.height );
			m.graphics.endFill();
			
			addChild( m );
			
			_chp.mask = m;
		}
		
		public function set text( s:String ):void
		{
			Tweener.removeTweens( _chp );
			_killTimer();
			
			var format:TextFormat = _chp.getTextFormat();
			
			_chp.text = s;
			
			_chp.setTextFormat( format );
			
			//_chp.x = 0;
			_doRollOut( null );
			
			_checkSize();
		}
		
		private function _checkSize():void
		{
			if ( _chp.width > _w ) {
				
				addEventListener( MouseEvent.ROLL_OVER, _doRollOver );
				addEventListener( MouseEvent.ROLL_OUT, _doRollOut );
				
				_setTimer();
				
			} else {
				
				removeEventListener( MouseEvent.ROLL_OVER, _doRollOver );
				removeEventListener( MouseEvent.ROLL_OUT, _doRollOut );
			}
		}
		
		// - - -
		
		private function _doRollOver( e:MouseEvent ):void
		{
			_killTimer();
			
			var duree:Number = - ( _w - _chp.width -_chp.x ) / 20;
			
			Tweener.removeTweens( _chp );
			Tweener.addTween( _chp, { x: _w - _chp.width, transition:"easeNone", time: duree } );
		}
		
		private function _doRollOut( e:MouseEvent ):void
		{
			var duree:Number = - _chp.x / 20;
			
			Tweener.removeTweens( _chp );
			//Tweener.addTween( _chp, { x: 0, transition:"easeNone", time: duree, onComplete: _setTimer} );
			Tweener.addTween( _chp, { x: 0, transition:"easeNone", time: duree, onComplete: null} );
		}
		
		// --- M O D E  A U T O
		
		private function _rollOver():void
		{
			var duree:Number = - ( _w - _chp.width -_chp.x ) / 20;
			
			Tweener.removeTweens( _chp );
			Tweener.addTween( _chp, { x: _w - _chp.width, transition:"easeNone", time: duree, onComplete: _rollOut } );
		}
		
		private function _rollOut():void
		{
			var duree:Number = - _chp.x / 20;
			
			Tweener.removeTweens( _chp );
			//Tweener.addTween( _chp, { x: 0, transition:"easeNone", time: duree, delay: 2, onComplete: _setTimer } );
			Tweener.addTween( _chp, { x: 0, transition:"easeNone", time: duree, delay: 2, onComplete: null } );
		}
		
		// - - -
		
		private function _setTimer():void 
		{
			if ( _auto == false ) return;
				
			_killTimer();
			
			_t = new Timer( 3500, 1 )
			_t.addEventListener( TimerEvent.TIMER_COMPLETE, _onTimerComplete );
			_t.start();
		}
		
		private function _killTimer():void 
		{
			if( _t != null ) {
				_t.stop();
				_t.removeEventListener( TimerEvent.TIMER_COMPLETE, _onTimerComplete );
				_t = null;
			}
		}
				
		private function _onTimerComplete( e:TimerEvent ):void
		{
			_killTimer();
			
			_rollOver();
		}
		
		
	}

}